package com.example.android.viper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Config implements java.io.Serializable {
	/**
	 * 
	 */
	private static String defaultPath = "assets/";
	private static String defaultFilename = "conf";
	private static Config conf=null;
	
	private static final long serialVersionUID = 1L;
	private String filename = defaultFilename;
	
	// Configuration file data
	public int currentLevel=1;		// Actual level reached
	
	// End of configuration file data
	
	public Config(String filen)
	{
		this.filename=filen;
	}
	
	public static Config State()
	{
		return conf;
	}
	
	public static void Load(String filen)
	{
		try
		{
			if(new File(defaultPath + filen).isFile())
			{
				FileInputStream fileIn = new FileInputStream(defaultPath + filen);
				ObjectInputStream in = new ObjectInputStream(fileIn);
				conf = (Config) in.readObject();
				in.close();
				fileIn.close();
			}
			else
			{
				conf = new Config(filen);
				Save();	
			}
			
			if(filen!=null)
			{
				conf.filename = filen;
			}			
		}
		catch(FileNotFoundException f)
		{
			f.printStackTrace();
			return;
		}
		catch(IOException i)
		{
			i.printStackTrace();
			return;
		}
		catch(ClassNotFoundException c)
		{
			System.out.println("Configuration class not found.");
			c.printStackTrace();
			return;
		}
	}
	
	public static void Load()
	{
		Load(defaultFilename);
	}
	
	public static void Save()
	{
		try
		{
			if(conf!=null)
			{
				FileOutputStream fileOut = new FileOutputStream(defaultPath + conf.filename);
				ObjectOutputStream out = new ObjectOutputStream(fileOut);
				out.writeObject(conf);
				out.close();
				fileOut.close();
			}
		}
		catch(IOException i)
		{
			i.printStackTrace();
		}
	}
}
